package com.lagou;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class SortReducer extends Reducer<IntWritable, NullWritable, NullWritable, Text> {
    private int order;
    private Text txt = new Text();
    //初始化序号数值0
    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        order = 0;
    }

    //reduce的key已经升序排列，key重复次数与values的长度相等，则迭代一次reducer写一次
    @Override
    protected void reduce(IntWritable key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        for (NullWritable value : values) {
            String s =  ++order + "\t" + key.toString();
            txt.set(s);
            context.write(NullWritable.get(), txt);
        }

    }
}
